
/**
 * name:        会员基础资料
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */

$(function(){

    
    if($.cookie('user')!=''&&$.cookie('user')!=null){ 
        var token=''
        isLogin();      
        getToken();     // 获取/验证token 
        if( token!='' && token!=null){
            $('.basic_box').append('<div class="none" style="width:100%;height:300px;background: #fff url(../../img/base/loading.gif) center center no-repeat;"></div>');
            getMemberBasicInfo();
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");  
            $('.nike_name').html(strs[1]) 
            token=strs[6];
            name=strs[1];
            phone=strs[2]; 
    }
    // 退出
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' });
            $.ajax({
                type:'post',
                url:new_url+'shop/login/logOut',
                dataType:'json',
                data:'',
                success:function(data){ 
                    tc(data.message) 
                }  
            })
            window.location.reload();
    })

    function getMemberBasicInfo(){
        $.ajax({
            type:'post',
            url:new_url+'shop/home/getMemberBasicInfo',
            dataType:'json',
            data:{token:token},  
            success:function(e){ 
                $('.none').remove();
                var memberBasicDataTpl = $("#memberBasicDataTpl").html();
                if(e.data.nickName==''||e.data.nickName===null){
                    e.data.nickName=name;
                }
                var birthdayOne = getLocalTime(e.data.birthday);
                var birthdayTwo = birthdayOne.replace(/ /,',').split(',');
                for(var k in birthdayTwo){
                    var birthdayThree = birthdayTwo[0].replace(/\//g,'-');
                }
                if(e.data.birthday){
                    e.data.birthday = birthdayThree;
                }else{
                    e.data.birthday='2017-11-05'    // 默认出生日期
                } 
                
                if(e.data.areaInfo != null){
                    var aeraOne = e.data.areaInfo;
                    var aeraTwo = aeraOne.split(',');
                    for(var j in aeraTwo){
                        var province = aeraTwo[0];
                        var city = aeraTwo[1];
                        var area= aeraTwo[2];
                    }
                    var memberBasicDataCont=template(
                        memberBasicDataTpl, {
                            memberBasicDataList: [{
                                province:province,
                                city:city,
                                area:area,
                                provinceId:e.data.provinceId,
                                cityId:e.data.cityId,
                                areaId:e.data.areaId,
                                areaInfo:e.data.areaInfo,
                                avatar:e.data.avatar,
                                birthday:e.data.birthday,
                                memberId:e.data.memberId,
                                name:e.data.name,
                                nickName:e.data.nickName,
                            }]
                        }
                    );
                    $('.basic_box').append(memberBasicDataCont);
                    $("#birthday").on("click",function(e){
                        e.stopPropagation();
                        $(this).lqdatetimepicker({
                            css : 'datetime-day',
                            dateType : 'D',
                            selectback : function(){}
                        });
                    }); 
                    /*地区信息*/
                    var cityParentId = (e.data.provinceId);
                    $.ajax({
                        type:'post',
                        url:new_url+'shop/home/areaInfo',
                        dataType:'json',
                        data:{token:token,parentId:cityParentId},
                        success:function(e){
                            for(var i=0;i<e.data.length;i++){
                                var t='<option value="'+e.data[i].area_id+'">'+e.data[i].area_name+'</option>';
                                $("#city").append(t);
                            }
                        }
                    });
                    var areaParentId = (e.data.cityId);
                    $.ajax({
                        type:'post',
                        url:new_url+'shop/home/areaInfo',
                        dataType:'json',
                        data:{token:token,parentId:areaParentId},
                        success:function(e){
                            for(var i=0;i<e.data.length;i++){
                                var t='<option value="'+e.data[i].area_id+'">'+e.data[i].area_name+'</option>';
                                $("#area").append(t);
                            }
                        }
                    });
                }else{
                    var province = '请选择';
                    var city = '请选择';
                    var area= '请选择';
                    var memberBasicDataCont=template(
                        memberBasicDataTpl, {
                            memberBasicDataList: [{
                                province:province,
                                city:city,
                                area:area,
                                areaInfo:e.data.areaInfo,
                                avatar:e.data.avatar,
                                birthday:e.data.birthday,
                                memberId:e.data.memberId,
                                name:e.data.name,
                                nickName:e.data.nickName,
                            }]
                        }
                    );
                    $('.basic_box').append(memberBasicDataCont);
                }
                

                if(e.data.sex==1){
                    $('#boy').attr('checked',true);
                }else{
                    $('#girl').attr('checked',true);
                } 
                setArea(token,URL);
                // 确认修改基本资料
                $('.basic_submit_btn').click(function(){
                    var nickName = $('.nickName').val();
                    var birthday = $('.birthday').val();
                    var provinceId = $('#province').val();
                    var cityId = $('#city').val();
                    var areaId = $('#area').val();
                    var areaInfo=$("#province").find("option:selected").text()+','+$("#city").find("option:selected").text()+','+$("#area").find("option:selected").text(); 
                    if($('#boy').attr('checked')){
                        var sex=1;
                    }else{
                        var sex=2;
                    };
                    var data={
                        token:token,
                        nickName:nickName,
                        birthday:birthday,
                        provinceId:provinceId,
                        cityId:cityId,
                        areaId:areaId,
                        areaInfo:areaInfo,
                        sex:sex
                    }
                    var mycars = new Array();
                    mycars[0] = e.data.name
                    mycars[1] = $('.nickName').val();
                    mycars[2] = phone
                    mycars[3] = e.data.avatar
                    mycars[4] = ''
                    mycars[5] = e.data.memberId
                    mycars[6] = token
                    $.ajax({
                        type:'post',
                        url:new_url+'shop/home/updateMemberInfo',
                        dataType:'json',
                        data:data,  
                        success:function(e){
                            $.cookie('user',mycars,{path:'/'});
                            window.location.reload();
                        }
                    })
                });
            }  
        });
    }

});

function getLocalTime(nS) {     
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, " ").replace(/日/g, " ");  
}